package com.hnevc.netfoodchoose;

import android.app.DownloadManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodDetailActivity extends AppCompatActivity {

    TextView tvFoodName;
    TextView tvFoodPrice;
    TextView tvFoodDesc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_detail);

        tvFoodName = (TextView) findViewById(R.id.id_tv_foodName);
        Intent intent = getIntent();
        final int foodID = intent.getIntExtra("foodID", 0);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String urls = "http://"
                            + getResources().getString(R.string.ServerIp)
                            + ":8080/RestaurantServer/Food/ShowFood" + foodID;
                    OkHttpClient client = new OkHttpClient.Builder()
                            .cookieJar(new MyCookieJar())
                            .build();

                    Request request = new Request.Builder()
                            .url(urls)
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String data = null;

                    data = response.body().string();
                    Gson gson = new Gson();
                    final Food food = gson.fromJson(data, Food.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvFoodName .setText(food.getFoodName());
                            tvFoodPrice.setText(food.getFoodPrice()+"");
                            tvFoodDesc.setText(food.getFoodDesc());
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        });

        String urls = "http://10.1.63.232:8080/RestaurantServer/Food/ShowFoos" + foodID;
    }
}
